export default class Point {
    x: number;
    y: number;
    g: number = 0;
    h: number = 0;
    f: number = 0;
    parent?: Point;
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    initF(endPoint: Point) {
        if (this.parent instanceof Point) {
            let g0 = this.calcG(this, this.parent);
            this.g = this.parent.g + g0;
            // 对角距离
            let dx = Math.abs(this.x - endPoint.x);
            let dy = Math.abs(this.y - endPoint.y);
            let [maxD, minD] = dx > dy ? [dx, dy] : [dy, dx];
            this.h = Math.SQRT2 * minD + (maxD - minD);
            this.f = this.g + this.h;
        }
    }

    calcG(n1: Point, n2: Point) {
        let g0 = Math.SQRT2;
        if (n1.x === n2.x || n1.y === n2.y) {
            g0 = 1;
        }
        return g0;
    }

    equals({ x, y }: Point) {
        return x === this.x && y === this.y;
    }
}
